﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataTransferObjects;
using VCN.App.DataAccessLayer;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace VCN.admin
{
    public partial class AddCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Thêm loại sản phẩm";
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (FileUpload.HasFile)
            {
                Categories v_Categories = new Categories();
                v_Categories.CategoryName = txt_CategoryName.Text;
                v_Categories.Image = this.doUploadPImage();
                CategoriesDAO v_CategoriesDAO = new CategoriesDAO();
                v_CategoriesDAO.insert(v_Categories);
                // Lấy toàn bộ các Category và add vào trong file XML
                string v_str_file_path = Server.MapPath("/home/");
                DataTable v_dt_all_cate = v_CategoriesDAO.selectAll2Dt();
                ghi_file_xml(v_str_file_path+"images.xml", v_dt_all_cate);
                Response.Redirect("CategoryList.aspx");
            }
            else lbl_Error.Visible = true;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txt_CategoryName.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("CategoryList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../Images/Product/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
            }
            return FileUpload.FileName;
        }
        private void ghi_file_xml(string ip_str_filename, DataTable ip_dt_category)
        {
            XmlTextWriter v_xml_writer = new XmlTextWriter(ip_str_filename, Encoding.UTF8);
            v_xml_writer.Formatting = Formatting.Indented;
            v_xml_writer.WriteStartDocument(); // images.xml
            v_xml_writer.WriteComment("This is the XML from which the SWF file reads the following data:\n"
            + "image - the thumb image in the menu.\n"
            + "url - the address to go to when clicking the image\n"
            + "target - _self - opens the url in the same browser page\n"
            + "- _blank - opens the url in a new browser page\n"
            + "- a number - executes _root.gotoAndPlay(number); in Flash\n"
            + "The tooltip text is recorded as CDATA. You can use HTML tags like '<br>');");
            // Bat dau write element Ly lich
            v_xml_writer.WriteStartElement("Carousel");
            v_xml_writer.WriteAttributeString("centeredImage", "imags/logo.png");
            v_xml_writer.WriteAttributeString("url", "/home/ListProducts.aspx");
            v_xml_writer.WriteAttributeString("target", "_blank");
            for (int v_i = 0; v_i < ip_dt_category.Rows.Count; v_i++)
            {
                v_xml_writer.WriteStartElement("photo");
                // Chỗ này là tên ảnh
                v_xml_writer.WriteAttributeString("image", "../Images/Product/" + ip_dt_category.Rows[v_i]["ANH_MINH_HOA"].ToString());
                v_xml_writer.WriteAttributeString("url", "/home/ListProducts.aspx?CateId=" + ip_dt_category.Rows[v_i]["ID"].ToString());
                v_xml_writer.WriteAttributeString("target", "_self");
                // Chỗ này ghi ghi chú cho loại sản phẩm
                v_xml_writer.WriteCData(Change_AV(ip_dt_category.Rows[v_i]["CATEGORY_NAME"].ToString()));
                v_xml_writer.WriteEndElement();
            }
            v_xml_writer.WriteEndElement(); // End element Carousel
            v_xml_writer.WriteEndDocument(); // End document images.xml
            v_xml_writer.Flush();
            v_xml_writer.Close();
        }
        public string Change_AV(string ip_str_change)
        {
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            return v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }
    }
}
